#!/usr/bin/python
from rap_core import *
from optparse import OptionParser
import os.path
import sys
import re

# *****************************************************************************
# main program
ul=[]
ul.append("usage: %prog [options] config")
ul.append("")
ul.append("Read in configuration string with n- and l-variables and output")
ul.append("the full set of configurations.  The term list and 2J-values for")
ul.append("each configuration can optionally be returned as well.")
usage=""
for u in ul: usage=usage+u+'\n'

parser=OptionParser(usage=usage)
parser.add_option("-t","--terms",action="store_true",dest="terms",\
                  default=False,help="print list of terms")
parser.add_option("-j","--j2vals",action="store_true",dest="j2vals",\
                  default=False,help="print list of 2J values")

(options,args)=parser.parse_args()
showterms=options.terms
showj2vals=options.j2vals

if len(args) == 0:
  parser.print_help()
  sys.exit(0)
else:
  confstr=' '.join(args)


# generate list of configurations
try:
  conflist=rlf_genconf(confstr)
except ErrorRAP as e:
  try:
    conflist=[rap_config(confstr)]
  except:
    print '*** Error - %s'%e.value
    sys.exit()


# print configuration list if no terms or 2J-values required
if not showterms and not showj2vals:
  for conf in conflist: print conf
  sys.exit()


# get list of terms
termlist={}
for conf in conflist: termlist[conf]=conf.allowed_terms()

# print configurations and terms if no 2J-values required
linelen=60
if not showj2vals:
  for conf in conflist:
    termkeys=termlist[conf].keys()
    termkeys.sort(rlf_termsorter)
    nterms=sum(termlist[conf].values())
    plt=''
    if nterms > 1: plt='s'
    print '%s     (%s term%s)'%(conf,nterms,plt)
    ts=[]
    for term in termkeys:
      mult=termlist[conf][term]
      if mult == 1:
        ts.append(term)
      else:
        ts.append('%s (x%s)'%(term,mult))
    line=ts.pop(0)
    while len(ts) > 0:
      line+=', '
      if len(line)+len(ts[0]) > linelen:
        print '  '+line
        line=''
      line+=ts.pop(0)
    print '  '+line
    print ''
  sys.exit()


# get list of 2J-values
if showj2vals:
  j2list={}
  for conf in conflist:
    for term in termlist[conf]:
      if term not in j2list: j2list[term]=rlf_term_2jvals(term)
half=False
if conflist[0].num_electrons()%2 == 1: half=True  # J-values are half-integers

# print terms and 2J-values
if showterms:
  for conf in conflist:
    termkeys=termlist[conf].keys()
    termkeys.sort(rlf_termsorter)
    nterms=sum(termlist[conf].values())
    nlevels=0
    for term in termkeys: nlevels+=termlist[conf][term]*len(j2list[term])
    plt=''
    pll=''
    if nterms > 1: plt='s'
    if nlevels > 1: pll='s'
    print '%s     (%s term%s, %s level%s)'%(conf,nterms,plt,nlevels,pll)
    for term in termkeys:
      mult=''
      if termlist[conf][term] > 1: mult='x%i'%termlist[conf][term]
      jlist=[]
      for j2 in j2list[term]:
        if half:
          jlist.append('%s/2'%j2)
        else:
          jlist.append('%s'%(j2/2))
      jlist=', '.join(jlist)
      print '  %s    %3s    J=%s'%(term,mult,jlist)
    print ''
  sys.exit()

# just print J-values
for conf in conflist:
  jlist={}
  for term,mult in termlist[conf].iteritems():
    for j2 in j2list[term]:
      if j2 not in jlist: jlist[j2]=0
      jlist[j2]+=mult
  nlevels=sum(jlist.values())
  pll=''
  if nlevels > 1: pll='s'
  print '%s     (%s level%s)'%(conf,nlevels,pll)
  jkeys=sorted(jlist.keys())
  tmp=[]
  for j2 in jkeys:
    if half:
      jstr='%s/2'%j2
    else:
      jstr='%s'%(j2/2)
    if jlist[j2] == 1:
      tmp.append(jstr)
    else:
      tmp.append('%s (x%s)'%(jstr,jlist[j2]))
  line='  J='+tmp.pop(0)
  while len(tmp) > 0:
    line+=', '
    if len(line)+len(tmp[0]) > linelen:
      print line
      line='    '
    line+=tmp.pop(0)
  print line
  print ''


